﻿using System;
using GameCreator.Camera;
using GameCreator.Core;
using GameCreator.Core.Hooks;
using Gp.Scripts.Character;
using Gp.Scripts.Core;
using Helpers;
using QFramework;
using UnityEngine;

namespace Gp.Scripts.Core
{
    public class PlayerRoot : SettleNodeRoot
    {
        [SerializeField] private GameObject playerObjects;

        public Camera MainCamera { get; private set; }
        public CameraFlowPivot CameraFlowPivot { get; private set; }
        public PlayerBaseCharacter PlayerCharacter { get; private set; }
        
        public CameraMotor MainCameraMotor { get; private set; }

        protected override void OnInit()
        {
            GameEntry.Instance.DontDestroy(gameObject);
            LoadCameraObjects();
            InitCameraObjects();
        }

        private void LoadCameraObjects()
        {
            CameraFlowPivot = ResHelper.LoadBasic<CameraFlowPivot>("CameraFlowPivot")
                .InstantiateWithParent(transform);
            MainCamera = ResHelper.LoadBasic<Camera>("CameraMain")
                .InstantiateWithParent(transform);
            MainCameraMotor = ResHelper.LoadBasic<CameraMotor>("CameraMotor")
                .InstantiateWithParent(transform);
        }

        public void InitCameraObjects()
        {
            var cameraMotor = MainCameraMotor;
            var cameraFollowPivot = CameraFlowPivot;
            var cameraController = MainCamera.GetComponentInChildren<CameraController>();
            cameraController.currentCameraMotor = cameraMotor;
            var motorType = cameraMotor.cameraMotorType as CameraMotorTypeFollow;
            
            motorType.anchor = new TargetGameObject(TargetGameObject.Target.GameObject) 
                { gameObject = cameraFollowPivot.gameObject };
            motorType.lookAt = new TargetDirection(TargetDirection.Target.Transform) 
                { targetTransform = cameraFollowPivot.transform };
        }

        public void LoadPlayerObject()
        {
            if (PlayerCharacter) Destroy(PlayerCharacter.gameObject);

            PlayerCharacter = ResHelper.LoadBasic<PlayerBaseCharacter>("MainCharacterTpl")
                .InstantiateWithParent(transform);
            PlayerCharacter.GetComponent<EntityBehaviour>().Initialize();
            PlayerCharacter.LoadModel();
            SyncCameraPos();
        }


        public void DisablePlayerObjects()
        {
            throw new NotImplementedException("Not implemented");
        }


        private void SyncCameraPos()
        {
            CameraFlowPivot.MainFollowTarget = PlayerCharacter.transform;
            var pos = PlayerCharacter.transform.position;
            HookCamera.Instance.transform.position = pos;
            CameraMotor.MAIN_MOTOR.transform.position = pos;
        }

        public void UnParent(Transform newParent)
        {
            playerObjects.transform.SetParent(newParent);
        }

        public void ReParent()
        {
            playerObjects.transform.SetParent(transform);
        }

        public void ToFocusMode()
        {
            
        }

        public void ToNormalMode()
        {
            
        }
    }
}